﻿
using log4net.Config;
using log4net;
using System.Reflection;

namespace AhCha.Fortunate.Common.Utility
{
    #region 方案一 自定义实现
    /// <summary>
    /// 日志输出工具类
    /// </summary>
    public class LogUtil
    {
        const string Logs = "Logs";
        /// <summary>
        /// 信息日志输出
        /// </summary>
        /// <param name="Content">输出的内容</param>
        /// <param name="directoryPath">输出的路径</param>
        public static void Info(string content, string directoryPath = "")
        {
            string fileName = string.Concat(DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath = Path.Combine(FileUtil.GetSystemDirectory, Logs, "Information");
            }
            IsExistDirectory(directoryPath, fileName);
            FileUtil.AppendText(Path.Combine(directoryPath, fileName), content + "\r\n \r\n");
        }

        /// <summary>
        /// 警告日志输出
        /// </summary>
        /// <param name="Content">输出的内容</param>
        /// <param name="directoryPath">输出的路径</param>
        public static void Warning(string content, string directoryPath = "")
        {
            string fileName = string.Concat(DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath = Path.Combine(FileUtil.GetSystemDirectory, Logs, "Warning");
            }
            IsExistDirectory(directoryPath, fileName);
            FileUtil.AppendText(Path.Combine(directoryPath, fileName), content + "\r\n \r\n");
        }

        /// <summary>
        /// 异常日志输出
        /// </summary>
        /// <param name="Content">输出的内容</param>
        /// <param name="directoryPath">输出的路径</param>
        public static void Error(string content, string directoryPath = "")
        {
            string fileName = string.Concat(DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath = Path.Combine(FileUtil.GetSystemLogPath, Logs, "Error");
            }
            IsExistDirectory(directoryPath, fileName);
            FileUtil.AppendText(Path.Combine(directoryPath, fileName), content + "\r\n \r\n");
        }

        /// <summary>
        /// 调式日志输出
        /// </summary>
        /// <param name="Content">输出的内容</param>
        /// <param name="directoryPath">输出的路径</param>
        public static void Debug(string content, string directoryPath = "")
        {
            string fileName = string.Concat(DateTime.Now.ToString("yyyy-MM-dd"), ".txt");
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath = Path.Combine(FileUtil.GetSystemDirectory, Logs, "Debug");
            }
            IsExistDirectory(directoryPath, fileName);
            FileUtil.AppendText(Path.Combine(directoryPath, fileName), content + "\r\n \r\n");
        }

        private static void IsExistDirectory(string directoryPath, string fileName)
        {
            FileUtil.CreateDirectory(directoryPath);
            FileUtil.CreateFile(Path.Combine(directoryPath, fileName));
        }
    }
    #endregion

    #region 方案二 Log4Net
    /// <summary>
    /// 日志输出工具类
    /// </summary>
    public static class Logger
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Logger));

        static Logger()
        {
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
        }

        public static void Info(string message)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
        }

        public static void Debug(string message)
        {
            if (log.IsDebugEnabled)
            {
                log.Debug(message);
            }
        }

        public static void Error(string message, Exception exception)
        {
            if (log.IsErrorEnabled)
            {
                log.Error(message, exception);
            }
        }

        public static void Warn(string message)
        {
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
        }

        public static void Fatal(string message, Exception exception)
        {
            if (log.IsFatalEnabled)
            {
                log.Fatal(message, exception);
            }
        }
    }
    #endregion
}